﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Globalization;
using System.Linq;
using System.Web.Mvc;
using Beeldbankmvc.Models;
using Beeldbankmvc.ViewModels;
using Beeldbankmvc.Code;

namespace Beeldbankmvc.Controllers
{
    [HandleErrorWithELMAH]
    public class ReportController : Controller
    {
        //
        // GET: /Report/
        [Authorize(Roles = "Administrator, Eindredacteur")]
        public ActionResult Index(string standardReport)
        {
            ReportRepository reportRepository = new ReportRepository();
            ReactionRepository reactionRepository = new ReactionRepository();
            OrderRepository orderRepository = new OrderRepository();

            DateTime fromDate = DateTime.Now.AddMonths(-3);
            DateTime toDate = DateTime.Now;
            if (standardReport != null)
            {
                switch (standardReport)
                {
                    case "CurMonth":
                        {
                            fromDate = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1);
                            toDate = DateTime.Now;
                            break;
                        }
                    case "PrevMonth":
                        {
                            fromDate = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1).AddMonths(-1);
                            toDate = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1).AddDays(-1);
                            break;
                        }
                    case "CurYear":
                        {
                            fromDate = new DateTime(DateTime.Today.Year, 1, 1);
                            toDate = DateTime.Now;
                            break;
                        }
                    case "PrevYear":
                        {
                            fromDate = new DateTime(DateTime.Today.Year, 1, 1).AddMonths(-12);
                            toDate = new DateTime(DateTime.Today.Year, 1, 1).AddDays(-1);
                            break;
                        }
                }
            }

            ReportPageViewModel pageViewModel = new ReportPageViewModel();
            pageViewModel.fromDate = fromDate;
            pageViewModel.toDate = toDate;

            pageViewModel.mostWantedModels = reportRepository.getMostWantedList(fromDate, toDate);
            pageViewModel.collectionNumbersModels = reportRepository.getCollectionNumbersList(fromDate, toDate);
            pageViewModel.recentlyAddedMediaModels = reportRepository.getRecentlyAddedMediaItemsList(fromDate, toDate, 10);
            pageViewModel.numberOfSearchesModel = reportRepository.getNumberOfSearches(fromDate, toDate);
            pageViewModel.orderModels = orderRepository.getSortedOrdersByTimeFrame(fromDate, toDate);

            return View(pageViewModel);
        }

        [HttpPost]
        [Authorize(Roles = "Administrator")]
        public ActionResult Index(ReportPageViewModel model)
        {
            ReportRepository reportRepository = new ReportRepository();
            ReactionRepository reactionRepository = new ReactionRepository();
            OrderRepository orderRepository = new OrderRepository();

            IFormatProvider culture = new CultureInfo("nl-NL", true);
            DateTime fromDate = DateTime.ParseExact(Request.Form["Report.From_Date"].ToString(), "dd-MM-yyyy", culture);
            DateTime toDate = DateTime.ParseExact(Request.Form["Report.To_Date"].ToString(), "dd-MM-yyyy", culture);

            ReportPageViewModel pageViewModel = new ReportPageViewModel();
            pageViewModel.fromDate = fromDate;
            pageViewModel.toDate = toDate;

            pageViewModel.mostWantedModels = reportRepository.getMostWantedList(fromDate, toDate);
            pageViewModel.collectionNumbersModels = reportRepository.getCollectionNumbersList(fromDate, toDate);
            pageViewModel.recentlyAddedMediaModels = reportRepository.getRecentlyAddedMediaItemsList(fromDate, toDate, 10);
            pageViewModel.numberOfSearchesModel = reportRepository.getNumberOfSearches(fromDate, toDate);
            pageViewModel.orderModels = orderRepository.getSortedOrdersByTimeFrame(fromDate, toDate);
            
            return View(pageViewModel);
        }

        //
        // GET: /Report/ReactionsPage
        [Authorize(Roles = "Administrator")]
        public ActionResult ReactionsPage(string standardReport, int? reactionPageIndex, int? mediareactionPageIndex)
        {
            ReportRepository reportRepository = new ReportRepository();
            ReactionRepository reactionRepository = new ReactionRepository();

            DateTime fromDate = DateTime.Now.AddMonths(-1);
            DateTime toDate = DateTime.Now;

            if (standardReport != null)
            {
                switch (standardReport)
                {
                    case "CurMonth":
                        {
                            fromDate = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1);
                            toDate = DateTime.Now;
                            break;
                        }
                    case "PrevMonth":
                        {
                            fromDate = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1).AddMonths(-1);
                            toDate = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1).AddDays(-1);
                            break;
                        }
                    case "CurYear":
                        {
                            fromDate = new DateTime(DateTime.Today.Year, 1, 1);
                            toDate = DateTime.Now;
                            break;
                        }
                    case "PrevYear":
                        {
                            fromDate = new DateTime(DateTime.Today.Year, 1, 1).AddMonths(-12);
                            toDate = new DateTime(DateTime.Today.Year, 1, 1).AddDays(-1);
                            break;
                        }
                }
            }

            ReportReactionsPageViewModel pageViewModel = new ReportReactionsPageViewModel();
            pageViewModel.fromDate = fromDate;
            pageViewModel.toDate = toDate;
            pageViewModel.pageSize = Constants.PAGE_SIZE;
            pageViewModel.ReactionPageIndex = 0;
            pageViewModel.MediaReactionPageIndex = 0;

            if (reactionPageIndex.HasValue)
                pageViewModel.ReactionPageIndex = reactionPageIndex.Value;
            if (mediareactionPageIndex.HasValue)
                pageViewModel.MediaReactionPageIndex = mediareactionPageIndex.Value;

            pageViewModel.ReactionStatusCountModels = reportRepository.getStatusCounts(fromDate, toDate);
            pageViewModel.ReactionTypeCountModels = reportRepository.getTypeCounts(fromDate, toDate);
            pageViewModel.ReactionCategoryCountModels = reportRepository.getCategoryCounts(fromDate, toDate);

            if (Request.QueryString["sort"] != null && Request.QueryString["sort"].Equals("status"))
            {
                pageViewModel.ReactionModel = reactionRepository.getSortedReactionsByTimeFrame(fromDate, toDate, pageViewModel.pageSize, pageViewModel.ReactionPageIndex, true).ToList();
                pageViewModel.numberOfRowsReaction = reactionRepository.getNumberOfReactionRows(fromDate, toDate);
            }
            else
            {
                pageViewModel.ReactionModel = reactionRepository.getSortedReactionsByTimeFrame(fromDate, toDate, pageViewModel.pageSize, pageViewModel.ReactionPageIndex, false).ToList();
                pageViewModel.numberOfRowsReaction = reactionRepository.getNumberOfReactionRows(fromDate, toDate);
            }

            if (Request.QueryString["sort"] != null && Request.QueryString["sort"].Equals("status"))
            {
                pageViewModel.MediaReactionModel = reactionRepository.getSortedMediaReactionsByTimeFrame(fromDate, toDate, pageViewModel.pageSize, pageViewModel.MediaReactionPageIndex, true).ToList();
                pageViewModel.numberOfRowsMediaReaction = reactionRepository.getNumberOfMediaReactionRows(fromDate, toDate);
            }
            else
            {
                pageViewModel.MediaReactionModel = reactionRepository.getSortedMediaReactionsByTimeFrame(fromDate, toDate, pageViewModel.pageSize, pageViewModel.MediaReactionPageIndex, false).ToList();
                pageViewModel.numberOfRowsMediaReaction = reactionRepository.getNumberOfMediaReactionRows(fromDate, toDate);
            }

            return View(pageViewModel);
        }

        //
        // POST: /Report/ReactionsPage
        [HttpPost]
        [Authorize(Roles = "Administrator")]
        public ActionResult ReactionsPage(ReportPageViewModel model, int? reactionPageIndex, int? mediareactionPageIndex)
        {
            ReportRepository reportRepository = new ReportRepository();
            ReactionRepository reactionRepository = new ReactionRepository();

            IFormatProvider culture = new CultureInfo("nl-NL", true);
            DateTime fromDate = DateTime.ParseExact(Request.Form["Report.From_Date"].ToString(), "dd-MM-yyyy", culture);
            DateTime toDate = DateTime.ParseExact(Request.Form["Report.To_Date"].ToString(), "dd-MM-yyyy", culture);

            ReportReactionsPageViewModel pageViewModel = new ReportReactionsPageViewModel();
            pageViewModel.fromDate = fromDate;
            pageViewModel.toDate = toDate;
            pageViewModel.pageSize = Constants.PAGE_SIZE;
            pageViewModel.ReactionPageIndex = 0;
            pageViewModel.MediaReactionPageIndex = 0;

            if (reactionPageIndex.HasValue)
                pageViewModel.ReactionPageIndex = reactionPageIndex.Value;
            if (mediareactionPageIndex.HasValue)
                pageViewModel.MediaReactionPageIndex = mediareactionPageIndex.Value;

            pageViewModel.ReactionStatusCountModels = reportRepository.getStatusCounts(fromDate, toDate);
            pageViewModel.ReactionTypeCountModels = reportRepository.getTypeCounts(fromDate, toDate);
            pageViewModel.ReactionCategoryCountModels = reportRepository.getCategoryCounts(fromDate, toDate);

            if (Request.QueryString["sort"] != null && Request.QueryString["sort"].Equals("status"))
            {
                pageViewModel.ReactionModel = reactionRepository.getSortedReactionsByTimeFrame(fromDate, toDate, pageViewModel.pageSize, pageViewModel.ReactionPageIndex, true).ToList();
                pageViewModel.numberOfRowsReaction = reactionRepository.getNumberOfReactionRows(fromDate, toDate);
            }
            else
            {
                pageViewModel.ReactionModel = reactionRepository.getSortedReactionsByTimeFrame(fromDate, toDate, pageViewModel.pageSize, pageViewModel.ReactionPageIndex, false).ToList();
                pageViewModel.numberOfRowsReaction = reactionRepository.getNumberOfReactionRows(fromDate, toDate);
            }

            if (Request.QueryString["sort"] != null && Request.QueryString["sort"].Equals("status"))
            {
                pageViewModel.MediaReactionModel = reactionRepository.getSortedMediaReactionsByTimeFrame(fromDate, toDate, pageViewModel.pageSize, pageViewModel.MediaReactionPageIndex, true).ToList();
                pageViewModel.numberOfRowsMediaReaction = reactionRepository.getNumberOfMediaReactionRows(fromDate, toDate);
            }
            else
            {
                pageViewModel.MediaReactionModel = reactionRepository.getSortedMediaReactionsByTimeFrame(fromDate, toDate, pageViewModel.pageSize, pageViewModel.MediaReactionPageIndex, false).ToList();
                pageViewModel.numberOfRowsMediaReaction = reactionRepository.getNumberOfMediaReactionRows(fromDate, toDate);
            }

            return View(pageViewModel);
        }

        public ActionResult MostWanted()
        {
            ReportRepository reportRepository = new ReportRepository();

            DateTime fromDate = DateTime.Now.AddDays(-5);
            DateTime toDate = DateTime.Now;

            IEnumerable<MostWantedModel> model = reportRepository.getMostWantedList(fromDate, toDate);

            return View(model);
        }

        [Authorize(Roles = "Administrator")]
        public ActionResult Orders()
        {
            return View();
        }

        public ActionResult RecentlyAddedMedia()
        {
            ReportRepository reportRepository = new ReportRepository();

            DateTime fromDate = DateTime.Now.AddMonths(-12);
            DateTime toDate = DateTime.Now;

            IEnumerable<RecentlyAddedMediaModel> model = reportRepository.getRecentlyAddedMediaItemsList(fromDate, toDate, 3);

            return View(model);
        }

        public ActionResult Numbers()
        {
            ReportRepository reportRepository = new ReportRepository();

            DateTime fromDate = new DateTime(1900, 1, 1);
            DateTime toDate = DateTime.Now;

            IQueryable<CollectionNumbersModel> model = reportRepository.getCollectionNumbersList(fromDate, toDate);

            return View(model);
        }

        [Authorize(Roles = "Administrator")]
        public ActionResult NumberOfSearches()
        {
            return View();
        }

        [Authorize(Roles = "Administrator, Eindredacteur")]
        public ActionResult ViewCounter(string standardReport, int? masteredPageIndex, int? unmasteredPageIndex, int? fotoPageIndex, int? bestandPageIndex)
        {
            ViewCounterViewModel viewCounterViewModel = new ViewCounterViewModel();

            DateTime fromDate = DateTime.Now.AddMonths(-1);
            DateTime toDate = DateTime.Now;

            if (standardReport != null)
            {
                switch (standardReport)
                {
                    case "CurMonth":
                        {
                            fromDate = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1);
                            toDate = DateTime.Now;
                            break;
                        }
                    case "PrevMonth":
                        {
                            fromDate = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1).AddMonths(-1);
                            toDate = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1).AddDays(-1);
                            break;
                        }
                    case "CurYear":
                        {
                            fromDate = new DateTime(DateTime.Today.Year, 1, 1);
                            toDate = DateTime.Now;
                            break;
                        }
                    case "PrevYear":
                        {
                            fromDate = new DateTime(DateTime.Today.Year, 1, 1).AddMonths(-12);
                            toDate = new DateTime(DateTime.Today.Year, 1, 1).AddDays(-1);
                            break;
                        }
                }
            }

            viewCounterViewModel.fromDate = fromDate;
            viewCounterViewModel.toDate = toDate;
            viewCounterViewModel.pageSize = Constants.PAGE_SIZE;
            viewCounterViewModel.currentPageIndexMastered = 0;
            viewCounterViewModel.currentPageIndexUnmastered = 0;
            viewCounterViewModel.currentPageIndexFoto = 0;
            viewCounterViewModel.currentPageIndexBestand = 0;

            if (masteredPageIndex.HasValue)
                viewCounterViewModel.currentPageIndexMastered = masteredPageIndex.Value;

            if (unmasteredPageIndex.HasValue)
                viewCounterViewModel.currentPageIndexUnmastered = unmasteredPageIndex.Value;

            if (fotoPageIndex.HasValue)
                viewCounterViewModel.currentPageIndexFoto = fotoPageIndex.Value;

            if (bestandPageIndex.HasValue)
                viewCounterViewModel.currentPageIndexBestand = bestandPageIndex.Value;


            ReportRepository repository = new ReportRepository();
            string publicationName = ConfigurationManager.AppSettings["publicationName"];

            viewCounterViewModel.viewCountersMastered = repository.getViewCounters(fromDate, toDate, true, viewCounterViewModel.pageSize, viewCounterViewModel.currentPageIndexMastered, publicationName);
            viewCounterViewModel.numberOfRowsMastered = repository.getNumberOfViewCounterRows(fromDate, toDate, true, publicationName);

            viewCounterViewModel.viewCountersUnmastered = repository.getViewCounters(fromDate, toDate, false, viewCounterViewModel.pageSize, viewCounterViewModel.currentPageIndexUnmastered, publicationName);
            viewCounterViewModel.numberOfRowsUnmastered = repository.getNumberOfViewCounterRows(fromDate, toDate, false, publicationName);

            viewCounterViewModel.viewCountersFoto = repository.getViewCountersFoto(fromDate, toDate, viewCounterViewModel.pageSize, viewCounterViewModel.currentPageIndexFoto, publicationName);
            viewCounterViewModel.numberOfRowsFoto = repository.getNumberOfViewCounterFotoRows(fromDate, toDate, publicationName);

            viewCounterViewModel.viewCountersBestand = repository.getViewCountersBestand(fromDate, toDate, viewCounterViewModel.pageSize, viewCounterViewModel.currentPageIndexBestand, publicationName);
            viewCounterViewModel.numberOfRowsBestand = repository.getNumberOfViewCounterBestandRows(fromDate, toDate, publicationName);

            return View(viewCounterViewModel);
        }

        [Authorize(Roles = "Administrator")]
        public ActionResult ViewAnalogAlbums(int? analogAlbumIndex)
        {
            ViewAnalogAlbumViewModel model = new ViewAnalogAlbumViewModel();
            // six in a row and 25 rows
            //model.pageSize = 6*25;
            model.pageSize = 4000;
            model.currentPageIndex = 0;

            if (analogAlbumIndex.HasValue)
                model.currentPageIndex = analogAlbumIndex.Value;

            ReportRepository repository = new ReportRepository();

            model.viewAnalogAlbums = repository.getAnalogAlbumList(model.pageSize, model.currentPageIndex);
            model.numberOfRowsAnalogAlbums = repository.getNumberOfAnalogAlbumRows();

            return View(model);
        }

        public ActionResult VideoOverView()
        {
            ReportRepository repository = new ReportRepository();
            repository.GenerateVideoOverView();
            return View();
        }

    }
}
